package org.easydarwin.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

public class NetWorkUtil {

	/**
	 * 检测当前网络是否已连接
	 * @param context
	 * @return
	 */
	public static boolean isNetworkConnected(Context context){
		if (context == null) {
			LogUtil.i("CommonUtils-isWap_context null");
			return false;
		}
		// 获得网络服务
		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connMgr == null) {
			LogUtil.i("CommonUtils-isWap_ConnectivityManager null");
			return false;
		}
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null) {
			LogUtil.i("CommonUtils-isWap_networkInfo null");
			return false;
		}
		if (!networkInfo.isConnected()){
			return false;
		}
		return true;
	}
	
	public static boolean isWifi(Context context){
        ConnectivityManager connectMgr = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectMgr.getActiveNetworkInfo();
        return info.getType() == ConnectivityManager.TYPE_WIFI;
    }
	

	public static boolean isWap(Context context) {
		LogUtil.i("checkNetWorkType_checkNetWorkType--> start!");
		// 网络类型
		int netType = -1;
		// WAP方式
		int WAP = 1;
		// NET方式
		int NET = 2;
		// WIFI方式
		int WIFI = 3;

		if (context == null) {
			LogUtil.i("CommonUtils-isWap_context null");
			return false;
		}
		// 获得网络服务
		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connMgr == null) {
			LogUtil.i("CommonUtils-isWap_ConnectivityManager null");
			return false;
		}
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null) {
			LogUtil.i("CommonUtils-isWap_networkInfo null");
			return false;
		}

		//
		int nType = networkInfo.getType();
		LogUtil.i("CommonUtils-isWap_nType=" + nType);
		LogUtil.i("networkInfo.getExtraInfo()_networkInfo.getExtraInfo() is--> "
				+ networkInfo.getExtraInfo());

		if (nType == ConnectivityManager.TYPE_MOBILE) {
			String proxyHost = android.net.Proxy.getDefaultHost();
			if (proxyHost != null && !proxyHost.equals("")) {
				// WAP方式
				netType = WAP;
			} else {
				netType = NET;
			}
			LogUtil.i("CommonUtils-isWap_proxyHost = " + proxyHost);
		} else if (nType == ConnectivityManager.TYPE_WIFI) {
			netType = WIFI;
		}
		LogUtil.i("CommonUtils-isWap_netType=" + netType);

		if (netType == 1) {
			LogUtil.i("CommonUtils-iswap_true");
			return true;
		} else {
			LogUtil.i("CommonUtils-iswap_false");
			return false;
		}
	}

}
